var table;
var layer;
var upload;
var form;
var laydate;
var noDelImg=[];
layui.use(['layer','table','upload','form','laydate','element'],function(){
    table=layui.table;
    layer=layui.layer;
    upload = layui.upload;
    form=layui.form;
   var element=layui.element;
    var laydate = layui.laydate;

    //创建人
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    $("#updateUser").val(create.user_id);

    //开始时间
    laydate.render({
        elem: '#startDate',
        type: 'datetime',
        trigger:'click',
        done:function (value,date) {
            $("#startDate").val(value);
        }
    });

    //结束时间
    laydate.render({
        elem: '#expireDate',
        type: 'datetime',
        trigger:'click',
        done:function (value,date) {
            $("#endDate").val(value);
        }
    });
    //获取路由跳转过来的课程·id
    var idArray = window.location.search.split("?");
    var carousel_id = idArray[1];
    selCarousel(carousel_id);
    //上传图片
    upload.render({
        elem : '#uploadImgBtn',
        url : '/SyOperationController/uoloadCourse',//上传接口
        done : function(res) {
            if(res.code==0){

                //截取查询出，删除图片
                var url=$("#img").attr("src");
                var delUrl=url.substr(url.lastIndexOf(':')+2);
                var jsonUrl={"delImg":delUrl};
                noDelImg.push(jsonUrl);

                $("#img").remove();
                $("#imgAddress").val(res.data);
                $("#img_div4").append("<img id='img' src='"+res.obj+"' style='width: 250px;height: 120px'>");
            }

        },
        drag : true,
        accept : 'file',
        multiple : true,
        data : {}
    });



    //上传视频
    upload.render({
        elem : '#uploadBtn2',
        url : '/SyOperationController/uoloadCourse',//上传接口
        done : function(res) {

            //截取查询出，删除视频
            var url=$(".uploadVideo").attr("src");
            var delUrl=url.substr(url.lastIndexOf(':')+2);
            var jsonUrl={"delImg":delUrl};
            noDelImg.push(jsonUrl);

            $("#my-video").remove();
            $("#div_video").append(" " +
                "<video id='my-video' class='video-js' controls preload='auto' data-setup='{}'> " +
                "<source src='"+res.obj+"' class='uploadVideo' type='video/mp4'/> " +
                "</video>"
            );
            //视频截取
            var arrayImgs=res.obj;
            var courseUrl=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
            $("#connect").val(courseUrl);
            var vid = document.getElementById("my-video");
            vid.onloadedmetadata = function() {
                var courseTime=Math.ceil(vid.duration)
                $("#courseTime").val(courseTime);
            };
            element.progress('progressBar',100 + '%');
            setTimeout("fun()","2000");  //2000毫秒后执行test()函数，只执行一次。
        },
        drag : true,
        accept : 'video',
        multiple : true,
        data : { }
        ,progress: function(value){
            $("#progressBar").attr("style","display:block");
            if(value<99){
                element.progress('progressBar',value + '%');
            }

        }
    });

});

function fun() {
    $("#progressBar").attr("style","display:none");
}

//获取内容
function selCarousel(carousel_id){
    $.ajax({
        url: "/SyOperationController/selCarousel",
        type: "post",
        dataType: "json",
        data:{"carousel_id":carousel_id},
        success: function (data) {
            $("#imgTitle").val(data.data[0].img_title);
            $("#carouselId").val(data.data[0].carousel_id);

            var start_date=data.data[0].start_date;
            start_date = start_date.slice(0, start_date.indexOf("."));
            start_date = start_date.replaceAll('T',' ');
            $("#startDate").val(start_date);

            var expire_date=data.data[0].expire_date;
            expire_date = expire_date.slice(0, expire_date.indexOf("."));
            expire_date = expire_date.replaceAll('T',' ');
            $("#expireDate").val(expire_date);

            var url=data.data[0].img_address;
            var imgAddress=url.substr(url.lastIndexOf(':')+2);
            $("#imgAddress").val(imgAddress);

            var connect=data.data[0].connect;
            $("#connect").val(connect);

            $("#remarks").val(data.data[0].remarks);

            $("#img_div4").append("<img src='"+data.data[0].img_address+"' id='img' style='width: 250px;height: 120px'/>");


            $("#div_video").append(" " +
                "<video id='my-video' class='video-js' controls preload='auto' data-setup='{}'> " +
                "<source src='"+data.data[0].connect+"' class='uploadVideo' type='video/mp4'/> " +
                "</video>"
            );




        }
    });
}


//返回
function go() {
    history.go(-1)
}

//刷新
function refresh() {
    location.reload();
}


//预览图点击展开
function uploadImg() {
    $("#up").attr("style","display:block");
    //预览图点击计数
    var num=0;
    num++;
    $("#num").val(num);
    $(".uploadImg2").attr("style","display:block");
    $("h1").remove(".h");
}
//收起预览图
function upUploadImg() {
    $(".uploadImg2").attr("style","display:none");
    $("#up").attr("style","display:none");
    $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
}

//修改轮播图
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){
        $.ajax({
            url:"/SyOperationController/updCarousel",
            type:"post",
            dataType:"json",
            data:data.field,
            success:function(data){
                if(data.code==0){
                    layer.msg("修改成功", {icon: 6})
                    cancels();
                }else{
                    layer.msg("修改失败", {icon: 5})
                }
            }
        });
        return false;
    });
});

//删除服务器图片
function cancels() {
    if(noDelImg.length>0){
        $.ajax({
            url:"/SyteacherSelController/noDelImg",
            type:"post",
            dataType:"json",
            traditional: true,
            data:{"noDelImg":JSON.stringify(noDelImg)},
            success:function(data){
                history.go(-1)
            }
        });
    }else{
        history.go(-1)
    }
}
